<?php

namespace App\Services\TransferGoldBean;

use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class TransferGoldBean
{
	/**
	 * @var User
	 */
	protected $self;

	/**
	 * @var User
	 */
	protected $child;

	protected $beans = 0;

	public function __construct(User $self, User $child, $beans)
	{
		$this->self = $self;
		$this->child = $child;
		$this->beans = $beans;
	}

	protected function transfer()
	{
		DB::transaction(function () {
			$data = ['child_user_id' => $this->child->id];
			account_beans($this->self)->minusTransferedBeans($this->beans, $data);

			$data = ['parent_user_id' => $this->self->id];
			account_beans($this->child)->plusTransferedBeans($this->beans, $data);
		});
	}

	public function apply()
	{

		$this->transfer();
	}
}
